<?php

// 广告位表

namespace app\console\model;
use think\Db;
use think\Model;
use think\Request;
use app\common\trilateral\Classify as ClassifyAlias;
use think\Session;
use think\Hook;

class Message extends Model
{
    // 模型事件
    protected static function init()
    {

        self::event('before_write', function ($data) {

            $data->pic_url = uploadImg('pic_url');

            auding($data);
        });
        self::event('after_write', function ($data) {
//            ["title"] => string(1) "1"
//            ["content"] => string(8) "<p>2</p>"
//            ["auding_status"] => int(1)
//            ["message_type"] => string(1) "1"
//            ["create_time"] => string(19) "2019-06-25 11:17:14"
//            ["update_time"] => string(19) "2019-06-25 11:17:14"
//            ["create_id"] => int(1)
//            ["id"] => string(2) "65"

            $member_message_list = Db::name('member_message')->where('type_id',1)->where('message_id',$data['id'])->select();
            if(Request::instance()->action() == 'create'){
                if($data['message_type'] == 1){
                    $member_list = Db::name('member')->field('id')->select()->toArray();//where('id',2969)->

                    $insertAll = [];
                    if(!empty($member_list)){
                        foreach ($member_list as $k => $v){
                            $insertAll[] = [
                                'member_id' => $v['id'],
                                'title' => $data['title'],
                                'content' => $data['content'],
                                'create_time' => $data['create_time'],
                                'message_id' => $data['id']
                            ];
                            $member_args[] = (string)($v['id']);

                            //(new \app\index\controller\MemberMessage())->jpushMessage(35,$data['title'],strip_tags($data['content']),1);
                        }
                       // $member_alias = implode(',',$member_args);

                        //(new \app\index\controller\MemberMessage())->jpushMessage_all($member_args,$data['title'],strip_tags($data['content']),1);

////
                        foreach ($member_list as &$v444){

                            $v444 = (string)$v444['id'];
                        }
                        $member_list2 = array_chunk($member_list, 1000);

                        //dump($data2);
                        $insertAll = array_chunk($insertAll,1000);
                        foreach ($insertAll as $vi){
                            (new \app\index\model\MemberMessage())->insertAll($vi);
                        }

                        foreach ($member_list2 as &$v123) {
                            // $v = implode(',', $v);
//            foreach($v as $v2){
//                $v2 = (string)$v2;
//            }
                            $content = strip_tags(str_replace('&nbsp;','',$data['content']));
                            (new \app\index\controller\MemberMessage())->jpushMessage_alias($v123, $data['title'], $content, 1);
                        }
////

                    }
                }

            }else{

                if(!empty($member_message_list)){
                    foreach ($member_message_list as $k => $v){
                        Db::name('member_message')->where('id',$v['id'])->update(
                            [
                                'title' => $data['title'],
                                'content' => $data['content']
                            ]
                        );
                       // (new \app\index\controller\MemberMessage())->jpushMessage($v['id'],$data['title'],$data['content'],1);
                    }
                }
            }

        });
    }

    protected function base($query)
    {
        $query->where('is_delete',1);
    }

    public function getAudingTextAttr($value, $data)
    {
        $status = [1 => '等待审核', 2 => '审核成功', 3 => '审核失败'];
        return $status[$data['auding_status']];
    }
//
    public function getAudingManageAttr($value, $data)
    {
       $ret = Db::name('manager')->where('id',$data['auding_id'])->value('username');
        return $ret;
    }
    public function getCreateManageAttr($value, $data)
    {
        $ret = Db::name('manager')->where('id',$data['create_id'])->value('username');
        return $ret;
    }
//    public function getNewsTypeAttr($value, $data)
//    {
//        $ret = Db::name('news_type')->where('id',$data['type_id'])->value('type_name');
//        return $ret;
//    }

}